package com.sun.mybatisplus.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auth: syh
 * @Date: 2021/10/22 14:30
 * @Email: 1014170249@qq.com
 * @Desc: 不在能知，而在能行
 * 演示队列的过期时间
 */
@Configuration
public class TTLConfiguration {

    /**
     * 注册Direct模式交换机
     * @return
     */
    @Bean
    public DirectExchange directExchangeTTL(){
        return new DirectExchange("ttl_direct_exchange",true,false,null);
    }

    /**
     * sms队列
     * @return
     */
    @Bean
    public Queue directTTLQueue(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-message-ttl",5000);  //设置过期时间5秒
        //设置队列长度为5，超过5条的数据就会放在死信队列中
        map.put("x-max-length",5);
        //队列过期后不会立马删除，会放入到死信队列中
        map.put("x-dead-letter-exchange","dead_direct_exchange");
        map.put("x-dead-letter-routing-key","dead");
        return new Queue("ttl_direct_queue",true,false,false,map);
    }



    @Bean
    public Queue directTTLMessageQueue(){
        return new Queue("ttlmessage_direct_queue",true);
    }

    /**
     * 通过路由key进行绑定
     * @return
     */
    @Bean
    public Binding directTTLMessageBinding(){
        return BindingBuilder.bind(directTTLMessageQueue()).to(directExchangeTTL()).with("ttlmessage");
    }


    /**
     * 通过路由key进行绑定
     * @return
     */
    @Bean
    public Binding directTTLBinding(){
        return BindingBuilder.bind(directTTLQueue()).to(directExchangeTTL()).with("ttl");
    }



}
